/**
 * 
 * 基于 Promise 对象
 */
import config from '@/config/index.config.js'
import http from './interface.js'
import tool from '../tool/tool.js'
module.exports = {
	request: (data) => {
		let url = data.url
		//设置请求前拦截器
		http.interceptor.request = (config) => {
			if (config.data.load) {
				//打开加载动画
				uni.showLoading();
			}
			if (uni.getStorageSync("yhToken")) {
				config.header.Authorization = uni.getStorageSync("yhToken")
			}
		}

		//设置请求结束后拦截器
		http.interceptor.response = async (res) => {
			
			if(res.data.data.token && url.indexOf("refreshToken") >= 0){
				
				if(res.data.code == 1){
					console.log('111111111111111111111', res, url)
					uni.setStorageSync("yhToken", res.data.data.token.access_token);
					uni.setStorageSync("expires_in", res.data.data.token.expires_in);
					uni.setStorageSync("refresh_expires_in", res.data.data.token.refresh_expires_in);
					let config = response.config
					config.header.Authorization = res.data.token.access_token
					var postData = {
						url: url,
						data: config.data,
						method: config.method
					}
					console.log('55555555555555', config)
					const resold = module.exports.request(postData)
					return resold
				}else{
					uni.removeStorageSync('yhToken');
					uni.removeStorageSync('expires_in');
					uni.removeStorageSync('refresh_expires_in');
				}
			}
			uni.hideLoading();
			/* if (res.statusCode !== 200 && res.statusCode !== 401 && res.statusCode !== 402 && res.statusCode !== 403) { //响应成功关闭loading图标
				
			}

			if (res.data.code == 401 || res.data.code == 402) {
				
			}
			if (res.data.code == 401) {
				return false;
			} */
			var nowTime = Number(Date.parse( new Date() ).toString().substr(0,10));
			var expires_in = Number(uni.getStorageSync("expires_in")) || 0;
			var refresh_expires_in = Number(uni.getStorageSync("refresh_expires_in")) || 0;
			// console.log(res.data);
			console.log(nowTime,  refresh_expires_in);
			if (res.data.code == 401 || (refresh_expires_in != 0 && nowTime > refresh_expires_in)) {
				tool.msg("请登录后操作");
				setTimeout(()=>{
					uni.redirectTo({
						url:`/pages/public/login`
					})
				}, 1000)
			}
			if(uni.getStorageSync("yhToken") && nowTime > expires_in - 10 && url.indexOf("/member/login/login" < 0 && nowTime < expires_in)){
				
				setTimeout(async () => {
					let token = uni.getStorageSync('yhToken')
					res.data = await doRequest(res, url, token)
					return;
				}, 2000)
			}
			/* if(res.data.code == 0){
				tool.msg(res.data.msg)
			} */
			
			return res.data;
		}
		return http.request(data)
	},
} //刷新token并继续之前请求
async function doRequest(response, url, token) {
	console.log("((((444444");
	const res = await module.exports.request({
		url: '/member/login/refreshToken',
		header: {
			'Content-Type': 'application/json;charset=UTF-8',
			'Content-Type': 'application/x-www-form-urlencoded',
			'Authorization': token
		},
		method: 'GET',
		data: {
			
		}
	})
}
